// 组件接口
import java.util.List;
import java.util.ArrayList;

interface Component {
    void operation();

    void add(Component leaf1);
}

// 叶节点类
class Leaf implements Component {
    private String name;

    public Leaf(String name) {
        this.name = name;
    }

    @Override
    public void operation() {
        System.out.println("Leaf " + name + " operation");
    }

    @Override
    public void add(Component leaf1) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'add'");
    }
}

// 组合节点类
class Composite implements Component {
    private List<Component> children = new ArrayList<>();

    public void add(Component component) {
        children.add(component);
    }

    public void remove(Component component) {
        children.remove(component);
    }

    @Override
    public void operation() {
        for (Component child : children) {
            child.operation();
        }
    }
}

// 客户端代码
public class CompositePatternDemo {
    public static void main(String[] args) {
        Component composite1 = new Composite();
        Component leaf1 = new Leaf("Leaf A");
        Component leaf2 = new Leaf("Leaf B");

        composite1.add(leaf1);
        composite1.add(leaf2);

        Component composite2 = new Composite();
        Component leaf3 = new Leaf("Leaf C");

        composite2.add(leaf3);
        composite2.add(composite1); // composite1 is also a child of composite2

        composite2.operation();
    }
}